import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, getInfo, loadSysVariables } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { UserState, UserForm } from '@/types/store'
import { Module } from 'vuex'

const baseUrl = config.baseUrl

const user: Module<UserState, UserState> = {
  state: {
    token: getToken(),
    userId: storage.get(constant.userId),
    username: storage.get(constant.username),
	deptName: storage.get(constant.deptName),
	deptId: storage.get(constant.deptId),
	easyName: storage.get(constant.easyName),
	deptCode: storage.get(constant.deptCode),
	gsbmId: storage.get(constant.gsbmId),
	jtgsDm: storage.get(constant.jtgsDm),
	jtgsId: storage.get(constant.jtgsId),
	jtgsMc: storage.get(constant.jtgsMc),
	zgldId: storage.get(constant.zgldId),
	zgldName: storage.get(constant.zgldName),
	id: storage.get(constant.id),
	monitorDept: storage.get(constant.monitorDept),
	monitorDeptCode: storage.get(constant.monitorDeptCode),
	phone: storage.get(constant.phone),
	photo: storage.get(constant.photo),
	roleCodes: storage.get(constant.roleCodes),
	roleIds: storage.get(constant.roleIds),
	roleNames: storage.get(constant.roleNames),
	rolesList: storage.get(constant.rolesList),
	zhDs: storage.get(constant.zhDs),
	zhId: storage.get(constant.zhId),
	zhMc: storage.get(constant.zhMc),
	zsldId: storage.get(constant.zsldId),
	zsldName: storage.get(constant.zsldName),
	zuoJi: storage.get(constant.zuoJi),
	sysVariables: storage.get(constant.SysVariables)
  },
  mutations: {
    SET_TOKEN: (state, token: string) => {
      state.token = token
    },
    SET_USERINFO: (state, userObj: Object) => {
		state.userId = userObj.userId; 
		storage.set(constant.userId, userObj.userId)
		
		state.username = userObj.username;
		storage.set(constant.username, userObj.username)
		
		state.deptName = userObj.deptName;
		storage.set(constant.deptName, userObj.deptName)
		
		state.deptId = userObj.deptId;
		storage.set(constant.deptId, userObj.deptId)
		
		state.easyName = userObj.easyName;
		storage.set(constant.easyName, userObj.easyName)
		
		state.deptCode = userObj.deptCode;
		storage.set(constant.deptCode, userObj.deptCode)
		
		state.gsbmId = userObj.gsbmId;
		storage.set(constant.gsbmId, userObj.gsbmId)
		
		state.jtgsDm = userObj.jtgsDm;
		storage.set(constant.jtgsDm, userObj.jtgsDm)
		
		state.jtgsId = userObj.jtgsId;
		storage.set(constant.jtgsId, userObj.jtgsId)
		
		state.jtgsMc = userObj.jtgsMc;
		storage.set(constant.jtgsMc, userObj.jtgsMc)
		
		state.zgldId = userObj.zgldId;
		storage.set(constant.zgldId, userObj.zgldId)
		
		state.zgldName = userObj.zgldName;
		storage.set(constant.zgldName, userObj.zgldName)
		
		state.id = userObj.id;
		storage.set(constant.id, userObj.id)
		
		state.monitorDept = userObj.monitorDept;
		storage.set(constant.monitorDept, userObj.monitorDept)
		
		state.monitorDeptCode = userObj.monitorDeptCode;
		storage.set(constant.monitorDeptCode, userObj.monitorDeptCode)
		
		state.phone = userObj.phone;
		storage.set(constant.phone, userObj.phone)
		
		state.photo = userObj.photo;
		storage.set(constant.photo, userObj.photo)
		
		state.roleCodes = userObj.roleCodes;
		storage.set(constant.roleCodes, userObj.roleCodes)
		
		state.roleIds = userObj.roleIds;
		storage.set(constant.roleIds, userObj.roleIds)
		
		state.roleNames = userObj.roleNames;
		storage.set(constant.roleNames, userObj.roleNames)
		
		state.rolesList = userObj.rolesList;
		storage.set(constant.rolesList, userObj.rolesList)
		
		state.zhDs = userObj.zhDs;
		storage.set(constant.zhDs, userObj.zhDs)
		
		state.zhId = userObj.zhId;
		storage.set(constant.zhId, userObj.zhId)
		
		state.zhMc = userObj.zhMc;
		storage.set(constant.zhMc, userObj.zhMc)
		
		state.zsldId = userObj.zsldId;
		storage.set(constant.zsldId, userObj.zsldId)
		
		state.zsldName = userObj.zsldName;
		storage.set(constant.zsldName, userObj.zsldName)
		
		state.zuoJi = userObj.zuoJi;
		storage.set(constant.zuoJi, userObj.zuoJi)
		
    },
	SET_SYSVARIABLES: (state, SysVariables: Object) => {
	  state.sysVariables = SysVariables
	  storage.set(constant.SysVariables, SysVariables)
	},
  },

  actions: {
    // 登录
    Login({ commit }, userInfo: UserForm) {
      return new Promise((resolve, reject) => { 
        login(userInfo).then((res: any) => {
          setToken(res.obj)
          commit('SET_TOKEN', res.obj)
		  // 查询系统变量
		  loadSysVariables("app").then((res) => {
			commit('SET_SYSVARIABLES', res.obj)
		  }).catch((e) => {
		  	console.log(e)
		  });
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then((res: any) => {
			// 将用户信息存到缓存
		  commit('SET_USERINFO', res)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then((res) => {
          commit('SET_TOKEN', '')
          removeToken()
          storage.clean()
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    }
  
  
  }
}

export default user
